import { IFeidaoAiNodejsPage } from '@feidao/web/interfaces';

import generate from '@feidao/web/atom/nodejs/url/query2url';
import is_online from '@feidao/web/atom/nodejs/msg/user-isonline';

import return_fail from '@feidao/web/atom/nodejs/server/return-fail';
import redirect from '@feidao/web/atom/nodejs/server/redirect';

function set_url(feidao_data: IFeidaoAiNodejsPage) {
	const url = feidao_data.data.url;
	const page_name = url.match(/.*\/((.*?)\.html)/)[1];
	const params = url.includes('?') ? url.substr(url.indexOf('?')) : '';
	const obj = {
		backurl: './' + page_name + params
	};
	return generate(obj, 'login.html');
}

export default async function check(fd: IFeidaoAiNodejsPage) {
	const { actionid, sessionid, spaceid, msg } = fd.data;
	const line = await is_online(actionid, sessionid, spaceid, msg);
	if (line) {

	} else {
		const url = set_url(fd);
		const r = redirect(url);
		return return_fail(r);
	}
}
